package com.bjpowernode.services.impls;

import com.bjpowernode.entities.TTran;
import com.bjpowernode.entities.TTranHistory;
import com.bjpowernode.entities.TUser;
import com.bjpowernode.mappers.TTranHistoryMapper;
import com.bjpowernode.mappers.TTranMapper;
import com.bjpowernode.querys.TranHistoryQuery;
import com.bjpowernode.services.TranHistoryService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class TranHistoryServiceImpl implements TranHistoryService {
    @Resource
    TTranHistoryMapper tTranHistoryMapper;
    @Resource
    TTranMapper tTranMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addTranHistory(TranHistoryQuery tranHistoryQuery) {
        TTranHistory tTranHistory = new TTranHistory();
        BeanUtils.copyProperties(tranHistoryQuery, tTranHistory);
        tTranHistory.setCreateTime(new Date());
        TUser tuser = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        tTranHistory.setCreateBy(tuser.getId());
        int insert = tTranHistoryMapper.insertSelective(tTranHistory);
        TTran tTran = new TTran();
        tTran.setId(tranHistoryQuery.getTranId());
        tTran.setStage(tranHistoryQuery.getStage());
        tTran.setExpectedDate(tranHistoryQuery.getExpectedDate());
        tTran.setEditTime(new Date());
        int update = tTranMapper.updateByIdSelective(tTran);
        return insert >= 1 && update >= 1;
    }
}
